////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   SearchPopup.java
// AUTHOR           :   ric.yik
// DATE CREATE      :   2006/5/19
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/5/19      ric.yik         1.0                 source is created.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.common.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.CommonUtil;

public class SearchPopup {

    private LinkedList criteriaList = new LinkedList();

    private ArrayList criteriaColumnList = new ArrayList();

    private ArrayList rsltTextList = new ArrayList();

    private ArrayList rsltColumnList = new ArrayList();

    private Hashtable tblParams = new Hashtable();

    private HashMap params = new HashMap();

    private String table;

    private String whereClause;

    private String groupBy;

    private String orderBy;

    private String sessionName;

    private String sessionValue;

    private String openerPath;

    private String pageEventSrc;

    private boolean exclusive;

    private List criteriaBuilders = new ArrayList();

    public interface SearchPopupBuilder {
        public SearchPopup build(SearchPopup popup);
    }

    public static class InputBox implements SearchPopupBuilder {

        private String label;

        private String defaultValue;

        private boolean withWildCard;

        public String getDefaultValue() {
            return defaultValue;
        }

        public InputBox setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
            return this;
        }

        public String getLabel() {
            return label;
        }

        public InputBox setLabel(String label) {
            this.label = label;
            return this;
        }

        public boolean isWithWildCard() {
            return withWildCard;
        }

        public InputBox setWithWildCard(boolean withWildCard) {
            this.withWildCard = withWildCard;
            return this;
        }

        public SearchPopup build(SearchPopup popup) {
            popup.getCriteriaColumnList().add(this.getLabel());
            popup.getCriteriaColumnList().add(this.isWithWildCard() ? "INPUT_WITH_WILDCARD" : "INPUT");
            popup.getCriteriaColumnList().add(this.getDefaultValue());
            return popup;
        }

    }

    public static class ComboBox implements SearchPopupBuilder {

        private String label;

        private String defaultValue;

        private boolean withWildCard;

        private String table;

        private String valueColumn;

        private String labelColumn;

        private String whereClause;

        private String orderBy;

        private boolean submitOnChange;

        public String getDefaultValue() {
            return defaultValue;
        }

        public ComboBox setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
            return this;
        }

        public String getLabel() {
            return label;
        }

        public ComboBox setLabel(String label) {
            this.label = label;
            return this;
        }

        public boolean isWithWildCard() {
            return withWildCard;
        }

        public ComboBox setWithWildCard(boolean withWildCard) {
            this.withWildCard = withWildCard;
            return this;
        }

        public String getLabelColumn() {
            return labelColumn;
        }

        public ComboBox setLabelColumn(String labelColumn) {
            this.labelColumn = labelColumn;
            return this;
        }

        public String getOrderBy() {
            return orderBy;
        }

        public ComboBox setOrderBy(String orderBy) {
            this.orderBy = orderBy;
            return this;
        }

        public boolean isSubmitOnChange() {
            return submitOnChange;
        }

        public ComboBox setSubmitOnChange(boolean submitOnChange) {
            this.submitOnChange = submitOnChange;
            return this;
        }

        public String getTable() {
            return table;
        }

        public ComboBox setTable(String table) {
            this.table = table;
            return this;
        }

        public String getValueColumn() {
            return valueColumn;
        }

        public ComboBox setValueColumn(String valueColumn) {
            this.valueColumn = valueColumn;
            return this;
        }

        public String getWhereClause() {
            return whereClause;
        }

        public ComboBox setWhereClause(String whereClause) {
            this.whereClause = whereClause;
            return this;
        }

        public SearchPopup build(SearchPopup popup) {
            popup.getCriteriaColumnList().add(this.getLabel());
            popup.getCriteriaColumnList().add("COMBO");
            popup.getCriteriaColumnList().add(this.getDefaultValue());
            popup.getCriteriaColumnList().add(this.getTable());
            popup.getCriteriaColumnList().add(this.getValueColumn());
            popup.getCriteriaColumnList().add(this.getLabelColumn());
            popup.getCriteriaColumnList().add(this.getWhereClause());
            popup.getCriteriaColumnList().add(this.getOrderBy());
            popup.getCriteriaColumnList().add(this.isSubmitOnChange() ? SimpleConstants.GENERIC_YES : "");
            return popup;
        }

    }

    public ArrayList getCriteriaColumnList() {
        return criteriaColumnList;
    }

    public SearchPopup setCriteriaColumnList(ArrayList criteriaColumnList) {
        this.criteriaColumnList = criteriaColumnList;
        return this;
    }

    public LinkedList getCriteriaList() {
        return criteriaList;
    }

    public SearchPopup setCriteriaList(LinkedList criteriaList) {
        this.criteriaList = criteriaList;
        return this;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public SearchPopup setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
        return this;
    }

    public String getGroupBy() {
        return groupBy;
    }

    public SearchPopup setGroupBy(String groupBy) {
        this.groupBy = groupBy;
        return this;
    }

    public String getOpenerPath() {
        return openerPath;
    }

    public SearchPopup setOpenerPath(String openerPath) {
        this.openerPath = openerPath;
        return this;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public SearchPopup setOrderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public String getPageEventSrc() {
        return pageEventSrc;
    }

    public SearchPopup setPageEventSrc(String pageEventSrc) {
        this.pageEventSrc = pageEventSrc;
        return this;
    }

    public HashMap getParams() {
        return params;
    }

    public SearchPopup setParams(HashMap params) {
        this.params = params;
        return this;
    }

    public ArrayList getRsltColumnList() {
        return rsltColumnList;
    }

    public SearchPopup setRsltColumnList(ArrayList rsltColumnList) {
        this.rsltColumnList = rsltColumnList;
        return this;
    }

    public ArrayList getRsltTextList() {
        return rsltTextList;
    }

    public SearchPopup setRsltTextList(ArrayList rsltTextList) {
        this.rsltTextList = rsltTextList;
        return this;
    }

    public String getSessionName() {
        return sessionName;
    }

    public SearchPopup setSessionName(String sessionName) {
        this.sessionName = sessionName;
        return this;
    }

    public String getSessionValue() {
        return sessionValue;
    }

    public SearchPopup setSessionValue(String sessionValue) {
        this.sessionValue = sessionValue;
        return this;
    }

    public String getTable() {
        return table;
    }

    public SearchPopup setTable(String table) {
        this.table = table;
        return this;
    }

    public Hashtable getTblParams() {
        return tblParams;
    }

    public SearchPopup setTblParams(Hashtable tblParams) {
        this.tblParams = tblParams;
        return this;
    }

    public String getWhereClause() {
        return whereClause;
    }

    public SearchPopup setWhereClause(String whereClause) {
        this.whereClause = whereClause;
        return this;
    }

    public SearchPopup addCriteria(String criteriaColumn, SearchPopupBuilder criteria) {
        this.getCriteriaColumnList().add(criteriaColumn);
        this.getCriteriaBuilders().add(criteria);
        return this;
    }

    public SearchPopup addResultHeader(String label) {
        this.getRsltTextList().add(label);
        return this;
    }

    public SearchPopup addResultColumn(String resultColumn) {
        this.getRsltColumnList().add(resultColumn);
        return this;
    }

    public SearchPopup build(HttpSession session) {
        for (Iterator builders = this.getCriteriaBuilders().iterator(); builders.hasNext();) {
            ((SearchPopupBuilder) builders.next()).build(this);
        }
        this.getTblParams().put(SimpleConstants.SESSION_TABLE, this.getTable());
        this.getTblParams().put(SimpleConstants.SESSION_WHERE, this.getWhereClause());
        if (!CommonUtil.isEmpty(this.getGroupBy())) this.getTblParams().put(SimpleConstants.SESSION_GROUPBY, this.getGroupBy());
        this.getTblParams().put(SimpleConstants.SESSION_ORDERBY, this.getOrderBy());
        this.getParams().put(SimpleConstants.SESSION_SESSION_NAME, this.getSessionName());
        this.getParams().put(SimpleConstants.SESSION_SESSION_VALUE, this.getSessionValue());
        this.getParams().put(SimpleConstants.SESSION_OPENER_PATH, this.getOpenerPath());
        this.getParams().put(SimpleConstants.SESSION_EVENTSRC, this.getPageEventSrc());
        this.getParams().put(SimpleConstants.SESSION_CRITERIA_LIST, this.getCriteriaList());
        this.getParams().put(SimpleConstants.SESSION_CRITERIA_COLUMN_LIST, this.getCriteriaColumnList());
        this.getParams().put(SimpleConstants.SESSION_RESULT_TEXT_LIST, this.getRsltTextList());
        this.getParams().put(SimpleConstants.SESSION_RESULT_COLUMN_LIST, this.getRsltColumnList());
        this.getParams().put(SimpleConstants.SESSION_TABLE_PARAMS, this.getTblParams());
        if (this.isExclusive()) this.getParams().put(SimpleConstants.SESSION_EXCLUSIVE, SimpleConstants.GENERIC_YES);
        session.setAttribute(SimpleConstants.SESSION_COMMON_SEARCH_PARAMETERS, this.getParams());
        return this;
    }

    public List getCriteriaBuilders() {
        return criteriaBuilders;
    }

    public SearchPopup setCriteriaBuilders(List criteriaBuilders) {
        this.criteriaBuilders = criteriaBuilders;
        return this;
    }

}
